home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / PIL / EpsImagePlugin.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.3 KB  |  296 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '0.5'
  5. import re
  6. import string
  7. import Image
  8. import ImageFile
  9.  
  10. def i32(c):
  11.     return ord(c[0]) + (ord(c[1]) << 8) + (ord(c[2]) << 16) + (ord(c[3]) << 24)
  12.  
  13.  
  14. def o32(i):
  15.     return chr(i & 255) + chr(i >> 8 & 255) + chr(i >> 16 & 255) + chr(i >> 24 & 255)
  16.  
  17. split = re.compile('^%%([^:]*):[ \\t]*(.*)[ \\t]*$')
  18. field = re.compile('^%[%!\\w]([^:]*)[ \\t]*$')
  19.  
  20. def Ghostscript(tile, size, fp):
  21.     '''Render an image using Ghostscript (Unix only)'''
  22.     (decoder, tile, offset, data) = tile[0]
  23.     (length, bbox) = data
  24.     import tempfile
  25.     import os
  26.     file = tempfile.mktemp()
  27.     command = [
  28.         'gs',
  29.         '-q',
  30.         '-g%dx%d' % size,
  31.         '-dNOPAUSE -dSAFER',
  32.         '-sDEVICE=ppmraw',
  33.         '-sOutputFile=%s' % file,
  34.         '- >/dev/null 2>/dev/null']
  35.     command = string.join(command)
  36.     
  37.     try:
  38.         gs = os.popen(command, 'w')
  39.         if bbox[0] != 0 or bbox[1] != 0:
  40.             gs.write('%d %d translate\n' % (-bbox[0], -bbox[1]))
  41.         
  42.         fp.seek(offset)
  43.         while length > 0:
  44.             s = fp.read(8192)
  45.             if not s:
  46.                 break
  47.             
  48.             length = length - len(s)
  49.             gs.write(s)
  50.         status = gs.close()
  51.         if status:
  52.             raise IOError('gs failed (status %d)' % status)
  53.         status
  54.         im = Image.core.open_ppm(file)
  55.     finally:
  56.         
  57.         try:
  58.             os.unlink(file)
  59.         except:
  60.             pass
  61.  
  62.  
  63.     return im
  64.  
  65.  
  66. class PSFile:
  67.     '''Wrapper that treats either CR or LF as end of line.'''
  68.     
  69.     def __init__(self, fp):
  70.         self.fp = fp
  71.         self.char = None
  72.  
  73.     
  74.     def __getattr__(self, id):
  75.         v = getattr(self.fp, id)
  76.         setattr(self, id, v)
  77.         return v
  78.  
  79.     
  80.     def seek(self, offset, whence = 0):
  81.         self.char = None
  82.         self.fp.seek(offset, whence)
  83.  
  84.     
  85.     def tell(self):
  86.         pos = self.fp.tell()
  87.         if self.char:
  88.             pos = pos - 1
  89.         
  90.         return pos
  91.  
  92.     
  93.     def readline(self):
  94.         s = ''
  95.         if self.char:
  96.             c = self.char
  97.             self.char = None
  98.         else:
  99.             c = self.fp.read(1)
  100.         while c not in '\r\n':
  101.             s = s + c
  102.             c = self.fp.read(1)
  103.         if c == '\r':
  104.             self.char = self.fp.read(1)
  105.             if self.char == '\n':
  106.                 self.char = None
  107.             
  108.         
  109.         return s + '\n'
  110.  
  111.  
  112.  
  113. def _accept(prefix):
  114.     if not prefix[:4] == '%!PS':
  115.         pass
  116.     return i32(prefix) == 0xC6D3D0C5L
  117.  
  118.  
  119. class EpsImageFile(ImageFile.ImageFile):
  120.     '''EPS File Parser for the Python Imaging Library'''
  121.     format = 'EPS'
  122.     format_description = 'Encapsulated Postscript'
  123.     
  124.     def _open(self):
  125.         fp = PSFile(self.fp)
  126.         s = fp.read(512)
  127.         if s[:4] == '%!PS':
  128.             offset = 0
  129.             fp.seek(0, 2)
  130.             length = fp.tell()
  131.         elif i32(s) == 0xC6D3D0C5L:
  132.             offset = i32(s[4:])
  133.             length = i32(s[8:])
  134.             fp.seek(offset)
  135.         else:
  136.             raise SyntaxError, 'not an EPS file'
  137.         (s[:4] == '%!PS').seek(offset)
  138.         box = None
  139.         self.mode = 'RGB'
  140.         self.size = (1, 1)
  141.         s = fp.readline()
  142.         while s:
  143.             if len(s) > 255:
  144.                 raise SyntaxError, 'not an EPS file'
  145.             len(s) > 255
  146.             if s[-2:] == '\r\n':
  147.                 s = s[:-2]
  148.             elif s[-1:] == '\n':
  149.                 s = s[:-1]
  150.             
  151.             
  152.             try:
  153.                 m = split.match(s)
  154.             except re.error:
  155.                 v = None
  156.                 raise SyntaxError, 'not an EPS file'
  157.  
  158.             if m:
  159.                 (k, v) = m.group(1, 2)
  160.                 self.info[k] = v
  161.                 if k == 'BoundingBox':
  162.                     
  163.                     try:
  164.                         box = map(int, map(float, string.split(v)))
  165.                         self.size = (box[2] - box[0], box[3] - box[1])
  166.                         self.tile = [
  167.                             ('eps', (0, 0) + self.size, offset, (length, box))]
  168.  
  169.                 
  170.             else:
  171.                 m = field.match(s)
  172.                 if m:
  173.                     k = m.group(1)
  174.                     if k == 'EndComments':
  175.                         break
  176.                     
  177.                     if k[:8] == 'PS-Adobe':
  178.                         self.info[k[:8]] = k[9:]
  179.                     else:
  180.                         self.info[k] = ''
  181.                 else:
  182.                     raise IOError, 'bad EPS header'
  183.                 s = m.readline()
  184.                 if s[:1] != '%':
  185.                     break
  186.                     continue
  187.         while s[0] == '%':
  188.             if len(s) > 255:
  189.                 raise SyntaxError, 'not an EPS file'
  190.             len(s) > 255
  191.             if s[-2:] == '\r\n':
  192.                 s = s[:-2]
  193.             elif s[-1:] == '\n':
  194.                 s = s[:-1]
  195.             
  196.             if s[:11] == '%ImageData:':
  197.                 (x, y, bi, mo, z3, z4, en, id) = string.split(s[11:], maxsplit = 7)
  198.                 x = int(x)
  199.                 y = int(y)
  200.                 bi = int(bi)
  201.                 mo = int(mo)
  202.                 en = int(en)
  203.                 if en == 1:
  204.                     decoder = 'eps_binary'
  205.                 elif en == 2:
  206.                     decoder = 'eps_hex'
  207.                 else:
  208.                     break
  209.                 if bi != 8:
  210.                     break
  211.                 
  212.                 if mo == 1:
  213.                     self.mode = 'L'
  214.                 elif mo == 2:
  215.                     self.mode = 'LAB'
  216.                 elif mo == 3:
  217.                     self.mode = 'RGB'
  218.                 else:
  219.                     break
  220.                 if id[-1:] == id[-1:]:
  221.                     pass
  222.                 elif id[-1:] == '"':
  223.                     id = id[1:-1]
  224.                 
  225.                 while None:
  226.                     s = fp.readline()
  227.                     if not s:
  228.                         break
  229.                     
  230.                     if s[:len(id)] == id:
  231.                         self.size = (x, y)
  232.                         self.tile2 = [
  233.                             (decoder, (0, 0, x, y), fp.tell(), 0)]
  234.                         return None
  235.                     continue
  236.             s[:len(id)] == id
  237.             s = fp.readline()
  238.             if not s:
  239.                 break
  240.                 continue
  241.         if not box:
  242.             raise IOError, 'cannot determine EPS bounding box'
  243.         box
  244.  
  245.     
  246.     def load(self):
  247.         if not self.tile:
  248.             return None
  249.         self.im = Ghostscript(self.tile, self.size, self.fp)
  250.         self.mode = self.im.mode
  251.         self.size = self.im.size
  252.         self.tile = []
  253.  
  254.  
  255.  
  256. def _save(im, fp, filename, eps = 1):
  257.     '''EPS Writer for the Python Imaging Library.'''
  258.     im.load()
  259.     if im.mode == 'L':
  260.         operator = (8, 1, 'image')
  261.     elif im.mode == 'RGB':
  262.         operator = (8, 3, 'false 3 colorimage')
  263.     elif im.mode == 'CMYK':
  264.         operator = (8, 4, 'false 4 colorimage')
  265.     else:
  266.         raise ValueError, 'image mode is not supported'
  267.     if im.mode == 'L':
  268.         fp.write('%!PS-Adobe-3.0 EPSF-3.0\n')
  269.         fp.write('%%Creator: PIL 0.1 EpsEncode\n')
  270.         fp.write('%%%%BoundingBox: 0 0 %d %d\n' % im.size)
  271.         fp.write('%%Pages: 1\n')
  272.         fp.write('%%EndComments\n')
  273.         fp.write('%%Page: 1 1\n')
  274.         fp.write('%%ImageData: %d %d ' % im.size)
  275.         fp.write('%d %d 0 1 1 "%s"\n' % operator)
  276.     
  277.     fp.write('gsave\n')
  278.     fp.write('10 dict begin\n')
  279.     fp.write('/buf %d string def\n' % im.size[0] * operator[1])
  280.     fp.write('%d %d scale\n' % im.size)
  281.     fp.write('%d %d 8\n' % im.size)
  282.     fp.write('[%d 0 0 -%d 0 %d]\n' % (im.size[0], im.size[1], im.size[1]))
  283.     fp.write('{ currentfile buf readhexstring pop } bind\n')
  284.     fp.write('%s\n' % operator[2])
  285.     ImageFile._save(im, fp, [
  286.         ('eps', (0, 0) + im.size, 0, None)])
  287.     fp.write('\n%%%%EndBinary\n')
  288.     fp.write('grestore end\n')
  289.     fp.flush()
  290.  
  291. Image.register_open(EpsImageFile.format, EpsImageFile, _accept)
  292. Image.register_save(EpsImageFile.format, _save)
  293. Image.register_extension(EpsImageFile.format, '.ps')
  294. Image.register_extension(EpsImageFile.format, '.eps')
  295. Image.register_mime(EpsImageFile.format, 'application/postscript')
  296.